home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / text / html / MinimalHTMLWriter.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-07-15  |  7.4 KB  |  337 lines

  1. package javax.swing.text.html;
  2.  
  3. import java.awt.Color;
  4. import java.io.IOException;
  5. import java.io.Writer;
  6. import java.util.Enumeration;
  7. import javax.swing.text.AbstractDocument;
  8. import javax.swing.text.AbstractWriter;
  9. import javax.swing.text.AttributeSet;
  10. import javax.swing.text.BadLocationException;
  11. import javax.swing.text.DefaultStyledDocument;
  12. import javax.swing.text.Element;
  13. import javax.swing.text.ElementIterator;
  14. import javax.swing.text.Style;
  15. import javax.swing.text.StyleConstants;
  16. import javax.swing.text.StyleContext;
  17. import javax.swing.text.StyledDocument;
  18.  
  19. public class MinimalHTMLWriter extends AbstractWriter {
  20.    private static final int BOLD = 1;
  21.    private static final int ITALIC = 2;
  22.    private static final int UNDERLINE = 4;
  23.    private static final CSS css = new CSS();
  24.    private int fontMask = 0;
  25.    int startOffset = 0;
  26.    int endOffset = 0;
  27.    private AttributeSet fontAttributes;
  28.  
  29.    public MinimalHTMLWriter(Writer var1, StyledDocument var2) {
  30.       super(var1, var2);
  31.    }
  32.  
  33.    public MinimalHTMLWriter(Writer var1, StyledDocument var2, int var3, int var4) {
  34.       super(var1, var2, var3, var4);
  35.    }
  36.  
  37.    protected void endFontTag() throws IOException {
  38.       ((AbstractWriter)this).write('\n');
  39.       this.writeEndTag("</font>");
  40.       this.fontAttributes = null;
  41.    }
  42.  
  43.    protected boolean inFontTag() {
  44.       return this.fontAttributes != null;
  45.    }
  46.  
  47.    protected boolean isText(Element var1) {
  48.       return var1.getName() == "content";
  49.    }
  50.  
  51.    private void setFontMask(AttributeSet var1) {
  52.       if (StyleConstants.isBold(var1)) {
  53.          this.fontMask |= 1;
  54.       }
  55.  
  56.       if (StyleConstants.isItalic(var1)) {
  57.          this.fontMask |= 2;
  58.       }
  59.  
  60.       if (StyleConstants.isUnderline(var1)) {
  61.          this.fontMask |= 4;
  62.       }
  63.  
  64.    }
  65.  
  66.    protected void startFontTag(String var1) throws IOException {
  67.       boolean var2 = false;
  68.       if (this.inFontTag()) {
  69.          this.endFontTag();
  70.          var2 = true;
  71.       }
  72.  
  73.       this.writeStartTag("<font style=\"" + var1 + "\">");
  74.       if (var2) {
  75.          ((AbstractWriter)this).indent();
  76.       }
  77.  
  78.    }
  79.  
  80.    protected void text(Element var1) throws IOException, BadLocationException {
  81.       String var2 = ((AbstractWriter)this).getText(var1);
  82.       if (var2.length() > 0 && var2.charAt(var2.length() - 1) == '\n') {
  83.          var2 = var2.substring(0, var2.length() - 1);
  84.       }
  85.  
  86.       if (var2.length() > 0) {
  87.          ((AbstractWriter)this).write(var2);
  88.       }
  89.  
  90.    }
  91.  
  92.    public void write() throws IOException, BadLocationException {
  93.       this.writeStartTag("<html>");
  94.       this.writeHeader();
  95.       this.writeBody();
  96.       this.writeEndTag("</html>");
  97.    }
  98.  
  99.    protected void writeAttributes(AttributeSet var1) throws IOException {
  100.       Enumeration var2 = var1.getAttributeNames();
  101.  
  102.       while(var2.hasMoreElements()) {
  103.          Object var3 = var2.nextElement();
  104.          if (var3 instanceof StyleConstants.ParagraphConstants || var3 instanceof StyleConstants.CharacterConstants || var3 instanceof StyleConstants.FontConstants || var3 instanceof StyleConstants.ColorConstants) {
  105.             ((AbstractWriter)this).indent();
  106.             ((AbstractWriter)this).write(var3.toString());
  107.             ((AbstractWriter)this).write(':');
  108.             ((AbstractWriter)this).write(css.styleConstantsValueToCSSValue((StyleConstants)var3, var1.getAttribute(var3)).toString());
  109.             ((AbstractWriter)this).write(';');
  110.             ((AbstractWriter)this).write('\n');
  111.          }
  112.       }
  113.  
  114.    }
  115.  
  116.    protected void writeBody() throws IOException, BadLocationException {
  117.       ElementIterator var1 = ((AbstractWriter)this).getElementIterator();
  118.       var1.current();
  119.       Object var2 = null;
  120.       this.writeStartTag("<body>");
  121.       boolean var3 = false;
  122.  
  123.       while((var4 = var1.next()) != null) {
  124.          if (((AbstractWriter)this).inRange(var4)) {
  125.             if (var4 instanceof AbstractDocument.BranchElement) {
  126.                if (var3) {
  127.                   this.writeEndParagraph();
  128.                   var3 = false;
  129.                   this.fontMask = 0;
  130.                }
  131.  
  132.                this.writeStartParagraph(var4);
  133.             } else if (this.isText(var4)) {
  134.                this.writeContent(var4, var3 ^ true);
  135.                var3 = true;
  136.             } else {
  137.                this.writeLeaf(var4);
  138.                var3 = true;
  139.             }
  140.          }
  141.       }
  142.  
  143.       if (var3) {
  144.          this.writeEndParagraph();
  145.       }
  146.  
  147.       this.writeEndTag("</body>");
  148.    }
  149.  
  150.    protected void writeComponent(Element var1) throws IOException {
  151.    }
  152.  
  153.    protected void writeContent(Element var1, boolean var2) throws IOException, BadLocationException {
  154.       AttributeSet var3 = var1.getAttributes();
  155.       this.writeNonHTMLAttributes(var3);
  156.       if (var2) {
  157.          ((AbstractWriter)this).indent();
  158.       }
  159.  
  160.       this.writeHTMLTags(var3);
  161.       this.text(var1);
  162.    }
  163.  
  164.    private void writeEndMask(int var1) throws IOException {
  165.       if (var1 != 0) {
  166.          if ((var1 & 1) != 0) {
  167.             ((AbstractWriter)this).write("</b>");
  168.          }
  169.  
  170.          if ((var1 & 2) != 0) {
  171.             ((AbstractWriter)this).write("</i>");
  172.          }
  173.  
  174.          if ((var1 & 4) != 0) {
  175.             ((AbstractWriter)this).write("</u>");
  176.          }
  177.       }
  178.  
  179.    }
  180.  
  181.    protected void writeEndParagraph() throws IOException {
  182.       this.writeEndMask(this.fontMask);
  183.       if (this.inFontTag()) {
  184.          this.endFontTag();
  185.       } else {
  186.          ((AbstractWriter)this).write('\n');
  187.       }
  188.  
  189.       this.writeEndTag("</p>");
  190.    }
  191.  
  192.    protected void writeEndTag(String var1) throws IOException {
  193.       ((AbstractWriter)this).decrIndent();
  194.       ((AbstractWriter)this).indent();
  195.       ((AbstractWriter)this).write(var1);
  196.       ((AbstractWriter)this).write('\n');
  197.    }
  198.  
  199.    protected void writeHTMLTags(AttributeSet var1) throws IOException {
  200.       int var2 = this.fontMask;
  201.       this.setFontMask(var1);
  202.       int var3 = 0;
  203.       int var4 = 0;
  204.       if ((var2 & 1) != 0) {
  205.          if ((this.fontMask & 1) == 0) {
  206.             var3 |= 1;
  207.          }
  208.       } else if ((this.fontMask & 1) != 0) {
  209.          var4 |= 1;
  210.       }
  211.  
  212.       if ((var2 & 2) != 0) {
  213.          if ((this.fontMask & 2) == 0) {
  214.             var3 |= 2;
  215.          }
  216.       } else if ((this.fontMask & 2) != 0) {
  217.          var4 |= 2;
  218.       }
  219.  
  220.       if ((var2 & 4) != 0) {
  221.          if ((this.fontMask & 4) == 0) {
  222.             var3 |= 4;
  223.          }
  224.       } else if ((this.fontMask & 4) != 0) {
  225.          var4 |= 4;
  226.       }
  227.  
  228.       this.writeEndMask(var3);
  229.       this.writeStartMask(var4);
  230.    }
  231.  
  232.    protected void writeHeader() throws IOException {
  233.       this.writeStartTag("<head>");
  234.       this.writeStartTag("<style>");
  235.       this.writeStartTag("<!--");
  236.       this.writeStyles();
  237.       this.writeEndTag("-->");
  238.       this.writeEndTag("</style>");
  239.       this.writeEndTag("</head>");
  240.    }
  241.  
  242.    protected void writeImage(Element var1) throws IOException {
  243.    }
  244.  
  245.    protected void writeLeaf(Element var1) throws IOException {
  246.       ((AbstractWriter)this).indent();
  247.       if (var1.getName() == "icon") {
  248.          this.writeImage(var1);
  249.       } else if (var1.getName() == "component") {
  250.          this.writeComponent(var1);
  251.       }
  252.  
  253.    }
  254.  
  255.    protected void writeNonHTMLAttributes(AttributeSet var1) throws IOException {
  256.       String var2 = "";
  257.       String var3 = "; ";
  258.       if (!this.inFontTag() || !this.fontAttributes.isEqual(var1)) {
  259.          Color var4 = (Color)var1.getAttribute(StyleConstants.Foreground);
  260.          if (var4 != null) {
  261.             var2 = var2 + "color: " + css.styleConstantsValueToCSSValue((StyleConstants)StyleConstants.Foreground, var4) + var3;
  262.          }
  263.  
  264.          Integer var5 = (Integer)var1.getAttribute(StyleConstants.FontSize);
  265.          if (var5 != null) {
  266.             var2 = var2 + "font-size: " + var5 + var3;
  267.          }
  268.  
  269.          String var6 = (String)var1.getAttribute(StyleConstants.FontFamily);
  270.          if (var6 != null) {
  271.             var2 = var2 + "font-family: " + var6 + var3;
  272.          }
  273.  
  274.          if (var2.length() > 0) {
  275.             this.startFontTag(var2);
  276.             this.fontAttributes = var1;
  277.          }
  278.  
  279.       }
  280.    }
  281.  
  282.    private void writeStartMask(int var1) throws IOException {
  283.       if (var1 != 0) {
  284.          if ((var1 & 4) != 0) {
  285.             ((AbstractWriter)this).write("<u>");
  286.          }
  287.  
  288.          if ((var1 & 2) != 0) {
  289.             ((AbstractWriter)this).write("<i>");
  290.          }
  291.  
  292.          if ((var1 & 1) != 0) {
  293.             ((AbstractWriter)this).write("<b>");
  294.          }
  295.       }
  296.  
  297.    }
  298.  
  299.    protected void writeStartParagraph(Element var1) throws IOException {
  300.       AttributeSet var2 = var1.getAttributes();
  301.       Object var3 = var2.getAttribute(StyleConstants.ResolveAttribute);
  302.       if (var3 instanceof StyleContext.NamedStyle) {
  303.          this.writeStartTag("<p class=" + ((StyleContext.NamedStyle)var3).getName() + ">");
  304.       } else {
  305.          this.writeStartTag("<p>");
  306.       }
  307.  
  308.    }
  309.  
  310.    protected void writeStartTag(String var1) throws IOException {
  311.       ((AbstractWriter)this).indent();
  312.       ((AbstractWriter)this).write(var1);
  313.       ((AbstractWriter)this).write('\n');
  314.       ((AbstractWriter)this).incrIndent();
  315.    }
  316.  
  317.    protected void writeStyles() throws IOException {
  318.       DefaultStyledDocument var1 = (DefaultStyledDocument)((AbstractWriter)this).getDocument();
  319.       Enumeration var2 = var1.getStyleNames();
  320.  
  321.       while(var2.hasMoreElements()) {
  322.          Style var3 = var1.getStyle((String)var2.nextElement());
  323.          if (var3.getAttributeCount() != 1 || !var3.isDefined(StyleConstants.NameAttribute)) {
  324.             ((AbstractWriter)this).indent();
  325.             ((AbstractWriter)this).write("p." + var3.getName());
  326.             ((AbstractWriter)this).write(" {\n");
  327.             ((AbstractWriter)this).incrIndent();
  328.             this.writeAttributes(var3);
  329.             ((AbstractWriter)this).decrIndent();
  330.             ((AbstractWriter)this).indent();
  331.             ((AbstractWriter)this).write("}\n");
  332.          }
  333.       }
  334.  
  335.    }
  336. }
  337.